package com.occludens.admin.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;

@Subclass(index=true)
@Cache
public class Portal extends AdminObjectUI {
	@Index private String link;
	@Load private List<Ref<Channel>> channels = new ArrayList<Ref<Channel>>();
	
	protected Portal() {} // Mandatory for Objectify
	
	public Portal(String name) {
		super(name);
	}
	
	public String getLink() {
		return this.link;
	}
	
	public void setLink(String link) {
		this.link = link;
	}
	
	public List<Channel> getChannels() {
		List<Channel> returnList = new ArrayList<Channel>();
		Iterator<Ref<Channel>> channelsItr = this.channels.iterator();
		while (channelsItr.hasNext()) {
			returnList.add(channelsItr.next().get());
		}//End of while channelsItr
		return returnList;
	}

	public void setChannels(List<Channel> channels) {
		Iterator<Channel> channelsItr = channels.iterator();
		while (channelsItr.hasNext()) {
			Ref<Channel> channelRef = Ref.create(channelsItr.next());
			if (!this.channels.contains(channelRef)) {
				this.channels.add(channelRef);
			}
		}//End of while channelsItr
	}

	public void addChannels(Channel... channels) {
		for (int i=0; i<channels.length; i++) {
			Ref<Channel> channelRef = Ref.create(channels[i]);
			if (!this.channels.contains(channelRef)) {
				this.channels.add(channelRef);
			}
		}
	}

	public void removeChannels(Channel... channels) {
		for (int i=0; i<channels.length; i++) {
			Ref<Channel> channelRef = Ref.create(channels[i]);
			if (this.channels.contains(channelRef)) {
				this.channels.remove(channelRef);
			}
		}
	}
}
